package com.central.apps.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.central.app.service.HandlerService;
import com.central.apps.dao.SysUserPersonDao;
import com.central.common.web.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = "系统用户")
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private SysUserPersonDao sysUserPersonDao;
    @GetMapping("/findPersonByDeptCode")
    @ApiOperation(value = "系统用户查询关联人员")
    public Map<String,Object> findChildTree(@RequestParam Map<String,Object> params){
        Map<String, Object> resultMap = new HashMap<>();
        String personid = MapUtils.getString(params, "personid");
        String name = MapUtils.getString(params, "name");
        String deptcode = MapUtils.getString(params, "deptcode");
        Integer pageNum = MapUtils.getInteger(params, "page");
        Integer pageSize = MapUtils.getInteger(params, "limit");
        if(StringUtils.isBlank(MapUtils.getString(params, "page")) && StringUtils.isBlank(MapUtils.getString(params, "limit"))){
            pageNum = 1;
            pageSize = 10;
        }
        int pageNumNew = pageNum - 1;
        int page = pageNumNew * pageSize;
//        String limitLimit = pageNumNew*pageSize+","+pageSize;

        if(StringUtils.isBlank(deptcode)){
            resultMap.put("code",1);
            resultMap.put("message","未获取到所属机构");
            resultMap.put("data",new ArrayList<>());
        }

        List<Map<String, Object>> bySqlCount = sysUserPersonDao.findSysUserPerson1(deptcode, personid,name);
        List<Map<String, Object>> bySql = sysUserPersonDao.findSysUserPerson(deptcode, page,pageSize,personid,name);


        //计算总条数
        int total = bySqlCount.size();
        resultMap.put("code", 0);
        resultMap.put("data", bySql);
        resultMap.put("count", total);

        return resultMap;
    }
}
